package com.ruoyi.record.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.record.domain.RecordBenefit;
import com.ruoyi.record.service.IRecordBenefitService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 保险金Controller
 * 
 * @author ruoyi
 * @date 2024-02-19
 */
@Controller
@RequestMapping("/record/benefit")
public class RecordBenefitController extends BaseController
{
    private String prefix = "record/benefit";

    @Autowired
    private IRecordBenefitService recordBenefitService;

    @RequiresPermissions("record:benefit:view")
    @GetMapping()
    public String benefit()
    {
        return prefix + "/benefit";
    }

    /**
     * 查询保险金列表
     */
    @RequiresPermissions("record:benefit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordBenefit recordBenefit)
    {
        startPage();
        List<RecordBenefit> list = recordBenefitService.selectRecordBenefitList(recordBenefit);
        return getDataTable(list);
    }

    /**
     * 新增保险金
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("benefitMonth", DateUtils.getMonth());
        return prefix + "/add";
    }

    /**
     * 新增保存保险金
     */
    @RequiresPermissions("record:benefit:add")
    @Log(title = "保险金", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordBenefit recordBenefit)
    {
        if(StringUtils.isEmpty(recordBenefit.getBenefitMonth())) {
            return error("新增失败，请选择保险金月份");
        }
        if(recordBenefitService.checkRecordBenefit(recordBenefit)) {
            recordBenefit.setCreateBy(ShiroUtils.getLoginName());
            return toAjax(recordBenefitService.insertRecordBenefit(recordBenefit));
        } else {
            return error("当前月份已存在保险金记录");
        }
    }

    /**
     * 修改保险金
     */
    @GetMapping("/edit/{benefitId}")
    public String edit(@PathVariable("benefitId") Long benefitId, ModelMap mmap)
    {
        RecordBenefit recordBenefit = recordBenefitService.selectRecordBenefitByBenefitId(benefitId);
        mmap.put("recordBenefit", recordBenefit);
        return prefix + "/edit";
    }

    /**
     * 修改保存保险金
     */
    @RequiresPermissions("record:benefit:edit")
    @Log(title = "保险金", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordBenefit recordBenefit)
    {
        if(StringUtils.isEmpty(recordBenefit.getBenefitMonth())) {
            return error("修改失败，请选择保险金月份");
        }
        if(recordBenefitService.checkRecordBenefit(recordBenefit)) {
            recordBenefit.setUpdateBy(ShiroUtils.getLoginName());
            return toAjax(recordBenefitService.updateRecordBenefit(recordBenefit));
        } else {
            return error("当前月份已存在保险金记录");
        }
    }

    /**
     * 删除保险金
     */
    @RequiresPermissions("record:benefit:remove")
    @Log(title = "保险金", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordBenefitService.deleteRecordBenefitByBenefitIds(ids));
    }
}
